import { defHttp } from '/@/utils/http/axios';
import {
  GetSpuListParamsModel,
  GetSpuListResponseModel,
  BaseSaleAttrListModel,
  SpuModel,
  SpuImageListModel,
  SpuSaleAttrListModel,
} from './model/spuModel';

// 定义请求地址的枚举
enum Api {
  GetSpuList = '/admin/product',
  GetBaseSaleAttrList = '/admin/product/baseSaleAttrList',
  SaveSpu = '/admin/product/saveSpuInfo',
  UpdateSpu = '/admin/product/updateSpuInfo',
  GetSpuSaleAttrList = '/admin/product/spuSaleAttrList',
  GetSpuImageList = '/admin/product/spuImageList',
}

/**
 * 获取属性列表
 * @param categoryIds 三级分类id对象
 * @returns
 */
export const getSpuListApi = ({ page, limit, category3Id }: GetSpuListParamsModel) => {
  return defHttp.get<GetSpuListResponseModel>({
    url: Api.GetSpuList,
    // page和limit位于params
    // 将来params中的参数会拼接到url后面
    params: `/${page}/${limit}`,
    // category3Id位于querystring
    // 将来data中的参数会放入params中
    data: {
      category3Id,
    },
  });
};

/**
 * 获取基础销售属性列表
 * @returns BaseSaleAttrListModel
 */
export const getBaseSaleAttrListApi = () => {
  return defHttp.get<BaseSaleAttrListModel>({
    url: Api.GetBaseSaleAttrList,
  });
};

/**
 * 添加SPU
 * @param spu spu对象
 * @returns null
 */
export const saveSpuApi = (spu: SpuModel) => {
  return defHttp.post<null>({
    url: Api.SaveSpu,
    data: spu,
  });
};

/**
 * 更新SPU
 * @param spu spu对象
 * @returns null
 */
export const updateSpuApi = (spu: SpuModel) => {
  return defHttp.post<null>({
    url: Api.UpdateSpu,
    data: spu,
  });
};

/**
 * 获取图片列表
 * @param spuId
 * @returns
 */
export const getSpuImageListApi = (spuId: number) => {
  return defHttp.get<SpuImageListModel>({
    url: Api.GetSpuImageList,
    params: `/${spuId}`,
  });
};

/**
 * 获取销售属性列表
 * @param spuId
 * @returns
 */
export const getSpuSaleAttrListApi = (spuId: number) => {
  return defHttp.get<SpuSaleAttrListModel>({
    url: Api.GetSpuSaleAttrList,
    params: `/${spuId}`,
  });
};
